package com.storyroad.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.storyroad.responseClasses.Results;
import com.storyroad.utils.DBConnection;
import com.storyroad.utils.TableNames;

/**
 * Servlet implementation class GetAllUsers
 */
public class GetAllUsers extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetAllUsers() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * Finds the users in the system whose usernames contain a specified keyword. An instance of
	 * {@link com.storyroad.responseClasses.Results} which contains the list of the matched usernames is returned to the
	 * front-end.
	 * @param request HttpServletRequest object that contains data coming from front-end
	 * @param response HttpServletResponse object that contains data which is prepared after processing the incoming data
	 * @throws IOException
	 */
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		PrintWriter out = response.getWriter();
		Connection conn = new DBConnection().connect();
		PreparedStatement stmt = null;
		ResultSet rs;
		ArrayList<String> result = new ArrayList<String>();
		
		// filter stores the keyword that will be searched within the usernames.
		String filter = request.getParameter("query");
		if (conn == null) {
			out.println("Connection error");
		} else {
			try {
				stmt = conn.prepareStatement("SELECT username FROM "
						+ TableNames.USERS_TABLE + " WHERE username LIKE ?");
				stmt.setString(1, "%"+filter+"%");
				rs = stmt.executeQuery();
				while (rs.next()) {
					result.add(rs.getString("username"));
				}
				
				Results users = new Results();
				users.setResults(result);
	
				JSONObject jsonObject = new JSONObject(users);
				String allUsersJsonString = jsonObject.toString();
				out.print(allUsersJsonString);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				out.print(e.getMessage());
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		processRequest(request, response);
	}

}
